<?php

namespace App\Console\Commands;


use App\Services\Classes\Youzan as YouzanRaw;
use App\Services\Facades\Youzan;
use App\Services\Models\Orders;
use Illuminate\Console\Command;

    

/**
 * DayTotal
 * 
 */
class PaymentReport extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'payment:report';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $orders = Orders::where('customs_status',0)->where('try_times','<',5)->get();
        if($orders->count() == 0){
            logger('there is no orders to report');
        }else{
            logger('start payment report task',$orders->toArray());
            foreach($orders as $order){
                $tid = $order->tid;
                $params = [
                    //'amount' => $payInfo['payment'],
                    'kdt_id' => YouzanRaw::kdt_id,
                    'customs_code' => 'CD',//已开放口岸列表 成都海关
                    'action_type' => 1,
                    'sub_order_no' => '', // 子订单号 , 拆单必传
                    'currency'  => 'CNY',
                    'transaction' => $order['transaction'],
                    'tid' => $order['tid'],
                ];
                $response = Youzan::reportPayment($params);
                if($response['code'] == 200){
                    $data = $response['data'];
                    $customs_status = $data['customs_status'];
                    $declare_customs_no = $data['declare_customs_no'];
                    Orders::where('tid',$tid)->update([
                        'customs_status' => $customs_status,
                        'declare_customs_no' => $declare_customs_no,
                        'report_result' => json_encode($response,JSON_UNESCAPED_UNICODE),
                        'try_times' => $order->try_times+1,
                    ]);
                }else{
                    Orders::where('tid',$tid)->update([
                        'code'    => $response['code'],
                        'msg'     => $response['message'],
                        'success' => $response['success'],
                        'report_result' => json_encode($response,JSON_UNESCAPED_UNICODE),
                        'try_times' => $order->try_times+1,
                    ]);
                }
               
            }
            logger('end payment report task');
        }
       
    }
}